package edu.pxxy.xw;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import edu.pxxy.lcc.daointerface.impl.FoodImpl;


public class MainActivity extends AppCompatActivity {
//GITEE:https://gitee.com/xu-wei/end_of_term
    private ListView listView;
    private ListViewAdapter adapter;
    private static final String TAG = "MainActivity...";

    /**
     * 用广播的方式来刷新数据  也可以用startActivityForResult来刷新数据
     */
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("main_receiver".equals(intent.getAction())) {
                refreshAdapter();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        insertDate();
        getData();
        initView();
        //注册广播
        IntentFilter filter = new IntentFilter();
        filter.addAction("main_receiver");
        registerReceiver(receiver, filter);
    }

    private void insertDate() {
        FoodImpl foodImpl3 = new FoodImpl(this);
        Food food = new Food();
        food.setFoodname("小炒肉");
        food.setFoodprice(20);
        food.setCount(0);
        foodImpl3.insertFood(food);
    }

    private List<Food> getData() {
        FoodImpl foodImpl = new FoodImpl(this);
        return  foodImpl.findAllFood();
    }

    private void initView() {
        listView = findViewById(R.id.list_view);
        adapter = new ListViewAdapter(getData(), this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this, ManageFoodActivity.class);
                startActivity(intent);
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                    @Override
                    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
                        contextMenu.add(1, 1, 1, "删除");
                    }
                });
                return false;
            }
        });
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        String id = String.valueOf(info.id);
        int i = Integer.parseInt(id);
        List<Food> foodList = getData();
        switch (item.getItemId()) {
            case 1:
                FoodImpl foodImpl = new FoodImpl(this);
                foodImpl.deleteFood(foodList.get(i).get_id());
                Log.d(TAG, "删除了id为" +foodList.get(i).get_id() + "的菜品");
                refreshAdapter();
                break;
        }
        return super.onContextItemSelected(item);
    }


    /**
     * 刷新recyclerView
     */
    private void refreshAdapter() {
        adapter = new ListViewAdapter(getData(), this);
        listView.setAdapter(adapter);
    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }
}
